Skip to content

Conversation

@AbdelrahmanHafez
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez commented Nov 30, 2025

PR Summary

Add TypeScript types and per-operation timestamps support for bulkWrite (gh-15782)

  • Added mongoose AnyBulkWriteOperation type with timestamps, overwriteImmutable, setDefaultsOnInsert, and skipValidation options
  • Fix current solution requiring timestamps: false to be able to update timestamps with overwriteImmutable.
  • Added JS and TS tests

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds per-operation timestamps support for insertOne and replaceOne in bulkWrite operations, along with comprehensive TypeScript type definitions for all bulkWrite operation types.

Key changes:

  • Introduced Mongoose-specific TypeScript types for bulkWrite operations that extend MongoDB driver functionality with options like timestamps, overwriteImmutable, setDefaultsOnInsert, and skipValidation
  • Implemented getTimestampsOpt helper to support per-operation timestamp control with proper precedence (operation-level > bulkWrite-level > default)
  • Added thorough test coverage for both JavaScript and TypeScript

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
types/models.d.ts Added custom TypeScript types for bulkWrite operations including AnyBulkWriteOperation, InsertOneModel, ReplaceOneModel, UpdateOneModel, UpdateManyModel, DeleteOneModel, and DeleteManyModel with Mongoose-specific options
test/types/models.test.ts Added TypeScript test gh15781 verifying type safety for timestamps and overwriteImmutable options on UpdateOneModel and UpdateManyModel; updated imports to distinguish between Mongoose and MongoDB driver types
test/model.test.js Added two comprehensive tests verifying per-operation timestamps functionality for insertOne and replaceOne operations with both timestamps: false and timestamps: true override scenarios
lib/helpers/model/castBulkWrite.js Implemented getTimestampsOpt helper function and integrated it into insertOne and replaceOne operations to support per-operation timestamp control

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a couple of minor comments

@AbdelrahmanHafez
Copy link
Collaborator Author

AbdelrahmanHafez commented Dec 3, 2025

@vkarpov15 The original solution required timestamps: false to work with updating timestamps. This PR also fixes that to make overwriteImmutable take effect on createdAt regardless of the timestamps option value. Also fixed in the PR that targets master #15819

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants